package com.zmc.coupon.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.zmc.common.annotation.PreventRepeatSubmit;
import com.zmc.common.enums.RequestSourceEnum;
import com.zmc.common.exception.base.WebDisplayException;
import com.zmc.common.utils.poi.ExcelUtil;
import com.zmc.common.utils.token.AuthTokenService;
import com.zmc.coupon.infrastructure.bean.query.ReductionQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zmc.coupon.infrastructure.entity.SkuFullReductionEntity;
import com.zmc.coupon.infrastructure.service.SkuFullReductionService;
import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.R;

import javax.servlet.http.HttpServletResponse;


/**
 * 商品满减信息
 *
 * @author zmc
 * @email 
 * @date 2024-12-04 15:37:00
 */
@RestController
@RequestMapping("/reduction")
public class SkuFullReductionController {
    
    @Autowired
    private SkuFullReductionService skuFullReductionService;

    @Autowired
    private AuthTokenService authTokenService;

    /**
     * 列表
     */
    @PostMapping("/list")
    public R list(@RequestBody ReductionQuery query){
        PageUtils page = skuFullReductionService.queryPage(query);
        return R.page(page);
    }

    /**
     * 导出列表
     */
    @PostMapping("/export")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public void export(HttpServletResponse response, ReductionQuery query) {
        List<SkuFullReductionEntity> list = skuFullReductionService.queryList(query);
        ExcelUtil<SkuFullReductionEntity> util = new ExcelUtil<>(SkuFullReductionEntity.class);
        util.exportExcel(response, list, "商品满减信息数据");
    }


    /**
     * 信息
     */
    @PostMapping("/info")
    public R info(@RequestBody SkuFullReductionEntity skuFullReduction){
        return R.ok(skuFullReductionService.getById(skuFullReduction.getId()));
    }

    /**
     * 保存
     */
    @PostMapping("/add")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R save(@RequestBody SkuFullReductionEntity skuFullReduction){
        SkuFullReductionEntity entity = skuFullReductionService.getBySkuId(skuFullReduction.getSkuId());
        if (entity != null) {
            throw new WebDisplayException("当前商品已参与其它满减活动,且一种商品sku只能参加一种满减活动");
        }
        skuFullReduction.setMerchantId(authTokenService.getUserInfo().getMerchantId());
        skuFullReductionService.save(skuFullReduction);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R update(@RequestBody SkuFullReductionEntity skuFullReduction) {
        SkuFullReductionEntity reduction = skuFullReductionService.getById(skuFullReduction.getId());
        if (!Objects.equals(reduction.getSkuId(), skuFullReduction.getSkuId())) {
            SkuFullReductionEntity entity = skuFullReductionService.getBySkuId(skuFullReduction.getSkuId());
            if (entity != null) {
                throw new WebDisplayException("当前商品已参与其它满减活动,且一种商品sku只能参加一种满减活动");
            }
        }
        skuFullReductionService.updateById(skuFullReduction);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R delete(@RequestBody SkuFullReductionEntity skuFullReduction){
		skuFullReductionService.removeById(skuFullReduction.getId());
        return R.ok();
    }

}
